﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace HuiZe.education
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException += (sender, args) =>
            {
                Log(args.Exception);
                args.SetObserved();
            };
            base.OnStartup(e);
            HuiZeeducation.Instance.StartHuiZeeducation();
        }

        void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            Log(e.Exception);
        }
        void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject != null)
            {
                if (e.ExceptionObject is Exception)
                    Log(e.ExceptionObject as Exception);
                else
                    Log(e.ExceptionObject.ToString());
            }
        }
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            Log(e.Exception);
        }

        public static void Log(Exception ex)
        {
            StringBuilder _sb = new StringBuilder();
            _sb.AppendLine($"message:{ex.Message}");
            _sb.AppendLine($"inner:{(ex.InnerException == null ? "" : ex.InnerException.Message)}");
            _sb.AppendLine($"source:{ex.Source}");
            _sb.AppendLine($"stacktrace:{ex.StackTrace}");
            _sb.AppendLine($"targetsite:{(ex.TargetSite == null ? "" : ex.TargetSite.ToString())}");
            Log(_sb.ToString());
        }

        public static void Log(string _error)
        {
            string _root = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
            if (!Directory.Exists(_root)) Directory.CreateDirectory(_root);

            string _path = Path.Combine(_root, $"{DateTime.Now.ToString("yyyy-MM-dd")}.txt");
            string _tag = string.Format("------------------------------{0}------------------------------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
            if (File.Exists(_path))
            {
                using (var _writer = new StreamWriter(_path, true, Encoding.UTF8))
                {
                    _writer.WriteLine(_tag);
                    _writer.WriteLine(_error);
                    _writer.WriteLine(_tag);
                    _writer.WriteLine(Environment.NewLine);
                    _writer.Close();
                }
            }
            else
            {
                File.WriteAllLines(_path, new string[4]
                {
                    _tag,_error,_tag,Environment.NewLine,
                }, Encoding.UTF8);
            }
        }

    }
}
